package view.panel;

import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;

import manager.CustomerManager;
import vo.Customer;
import exception.CannotModifException;
import exception.NotFoundException;
import exception.OverFlowException;

public class CustomerPanel extends JPanel {

	JTextField jfieldCustomerInfo, jfieldYear, jfieldMonth, jfieldDay, 
	jfieldCustomerId, jfieldCustomerName, jfieldPhone, jfieldBirth;
	JButton jbtnSearchCustomer, jbtnNewCustomer, jbtnSaveCustomer, jbtnDelCustomer;
	JLabel jlabelRegDate, jlabelYear, jlabelMonth, jlabelDay, jlabelCustomId, jlabelCustomName,
	jlabelPhone, jlabelBirth;
	int originId = 0;

	public CustomerPanel() {

		setLayout(new FlowLayout(FlowLayout.LEFT, 10, 30));
		setSize(300,200);
		setVisible(true);

		jfieldBirth = new JTextField("생년월일",12);
		jfieldCustomerId = new JTextField("고객번호",8);
		jfieldCustomerName = new JTextField("고객명",4);
		jfieldDay = new JTextField("일",4);
		jfieldMonth = new JTextField("월",4);
		jfieldYear = new JTextField("년",4);
		jfieldPhone = new JTextField("전화번호",16);
		jfieldCustomerInfo = new JTextField("고객정보",8);

		jbtnNewCustomer = new JButton("새 고객");
		jbtnSaveCustomer = new JButton("저장");
		jbtnSearchCustomer = new JButton("고객정보검색");
		jbtnDelCustomer = new JButton("삭제");

		jlabelBirth = new JLabel("생일");
		jlabelCustomId = new JLabel("고객번호");
		jlabelCustomName = new JLabel("고객명");
		jlabelYear = new JLabel("년");
		jlabelMonth = new JLabel("월");
		jlabelDay = new JLabel("일");
		jlabelPhone = new JLabel("전화번호");
		jlabelRegDate = new JLabel("가입일");

		add(jfieldCustomerInfo);

		add(jlabelRegDate);		
		add(jfieldYear);		
		add(jlabelYear);
		add(jfieldMonth);
		add(jlabelMonth);
		add(jfieldDay);
		add(jlabelDay);
		add(jlabelCustomId);
		add(jfieldCustomerId);
		add(jlabelCustomName);
		add(jfieldCustomerName);
		add(jlabelPhone);
		add(jfieldPhone);
		add(jlabelBirth);
		add(jfieldBirth);


		add(jbtnSearchCustomer);
		add(jbtnNewCustomer);
		add(jbtnDelCustomer);
		add(jbtnSaveCustomer);

		jbtnNewCustomer.addMouseListener(new MouseInputAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				String blank = "";
				
				// UI 초기화
				jfieldYear.setText(blank);
				jfieldPhone.setText(blank);
				jfieldMonth.setText(blank);
				jfieldDay.setText(blank);
				jfieldCustomerName.setText(blank);
				jfieldCustomerId.setText(blank);
				jfieldBirth.setText(blank);
				
				invalidate();

			}

		});

		jbtnSaveCustomer.addMouseListener(new MouseInputAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				
		
				// UI 로부터 정보를 받음
				String customerName = jfieldCustomerName.getText();
				String customerId = jfieldCustomerId.getText();
				String customerBirthDate = jfieldBirth.getText();
				String	customerRegDate = jfieldYear.getText() + jfieldMonth + jfieldDay;
				String customerPhone = jfieldPhone.getText();
							
				if(originId != Integer.parseInt(jfieldCustomerId.getText()) && originId != 0){
					try {
						// 처음 저장된 고객정보와 다를 경우 익셉션 처리
						throw new CannotModifException();
					} catch (CannotModifException e1) {
						e1.printStackTrace();
					}
				}
				// TODO
				// if(!customerRegDate.contains("-") )
				
				if(customerName.length() > 10)
					try {
						throw new OverFlowException();
					} catch (OverFlowException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				
				Customer customer = new Customer();

				// 고객 Object에 정보 저장
				customer.setName(customerName);
				customer.setId(Integer.parseInt(customerId));
				customer.setBirthDate(jfieldBirth.getText());
				customer.setRegYear(jfieldYear.getText());
				customer.setRegMonth(jfieldMonth.getText());
				customer.setRegDay(jfieldDay.getText());	
				customer.setTelPhone(customerPhone);

				// 고객 등록
				CustomerManager.getInstance().registCustomer(customer);
			}

		});

		jbtnDelCustomer.addMouseListener(new MouseInputAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					// 고객 번호로 검색하여 고객 정보 삭제
					CustomerManager.getInstance().unregistCustomer(
							Integer.parseInt(jfieldCustomerId.getText()));
				} catch (NumberFormatException e1) {
					e1.printStackTrace();
				} catch (NotFoundException e1) {
					e1.printStackTrace();
				}
			}

		});


		jbtnSearchCustomer.addMouseListener(new MouseInputAdapter() { 
			@Override
			public void mouseClicked(MouseEvent e) {

				int findCustomerId = Integer.parseInt(jfieldCustomerInfo.getText());		
				originId = findCustomerId;
				
				Customer customer;

				String regYear = null, regMonth = null, regDay = null;
				String customerName = null;
				int customerId = 0;
				String phone = null;
				String birthDate = null;


				try {
					// 입력된 고객 번호로 리스트에서 곡객을 검색 
					customer = CustomerManager.getInstance().findCustomer(findCustomerId);

					regYear = customer.getRegYear();
					regMonth = customer.getRegMonth();
					regDay = customer.getRegDay();
					customerName = customer.getName();
					customerId = customer.getId();
					phone = customer.getTelPhone();
					birthDate = customer.getBirthDate();		

				} catch (NotFoundException e1) {
					// 정보가 없는 경우 익셉션 처리
					e1.printStackTrace();
				}

				// UI에 표시
				jfieldCustomerName.setText(customerName);
				jfieldCustomerId.setText(String.valueOf(customerId));
				jfieldBirth.setText(birthDate);
				jfieldYear.setText(regYear);
				jfieldMonth.setText(regMonth);
				jfieldDay.setText(regDay);
				jfieldPhone.setText(phone);
				invalidate();
			}
		});
	}

}
